//
//  UIKeyboardViewController.m
//
//
//  Created by  YFengchen on 13-1-4.
//  Copyright 2013 __zhongyan__. All rights reserved.
//

#import "UIKeyboardViewController.h"

static CGFloat kboardHeight = 254.0f;
static CGFloat keyBoardToolbarHeight = 44.0f;
static CGFloat spacerY = 10.0f;
static CGFloat viewFrameY = 0;

@interface UIKeyboardViewController ()

- (void)animateView:(BOOL)isShow textField:(id)textField heightforkeyboard:(CGFloat)kheight;
- (void)addKeyBoardNotification;
- (void)removeKeyBoardNotification;
- (void)checkBarButton:(id)textField;
- (id)firstResponder:(UIView *)navView;
- (NSArray *)allSubviews:(UIView *)theView;

@end

@implementation UIKeyboardViewController

@synthesize boardDelegate = _boardDelegate;

- (void)dealloc {
    _boardDelegate = nil;
    [self removeKeyBoardNotification];
}

//监听键盘隐藏和显示事件
- (void)addKeyBoardNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillHideNotification object:nil];
}

//注销监听事件
- (void)removeKeyBoardNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

//计算当前键盘的高度
-(void)keyboardWillShowOrHide:(NSNotification *)notification {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
#endif
        kboardHeight = 264.0f + keyBoardToolbarHeight;
    }
    NSValue *keyboardBoundsValue;
    //	if (IOS_VERSION >= 3.2) {
    keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    //	}
    //	else {
    //		keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
    //	}
    [keyboardBoundsValue getValue:&keyboardBounds];
    BOOL isShow = [[notification name] isEqualToString:UIKeyboardWillShowNotification] ? YES : NO;
    //if ([self firstResponder:objectView]) {
        //        if ( isShow ) {  //这个地方会被调用好多次，比如UITextView，每增加一行，都会调用一次
        //            viewFrameY = objectView.frame.origin.y; //先记录view的位置
        //        }
        [self animateView:isShow textField:[self firstResponder:objectView]
        heightforkeyboard:keyboardBounds.size.height];
    //}
}

//输入框上移防止键盘遮挡
- (void)animateView:(BOOL)isShow textField:(id)textField heightforkeyboard:(CGFloat)kheight {
    //    if (![UIApplication sharedApplication].keyWindow.bounds.size.height ==480) {
    
    //    if (!self.textFieldMoveOrNot) {
    //        return;
    //    }
    
    kboardHeight = kheight;
    [self checkBarButton:textField];
    CGRect rect = objectView.frame;
    //夏冰修改再次预约bug
    //    CGRect rect = VCView.frame;
    rect.size.height += _tabbarHeight ;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    
    CGFloat objViewSpaceY = 0 ;
    if (isShow) {
        CGPoint textPoint  ;
        if ([textField isKindOfClass:[UITextField class]]) {
            UITextField *newText = ((UITextField *)textField);
            textPoint = [newText convertPoint:CGPointMake(0, newText.frame.size.height + spacerY) toView:objectView];
            //            CGPoint textPoint = [newText convertPoint:CGPointMake(0, newText.frame.size.height + spacerY) toView:VCView];
            
            objViewSpaceY = rect.size.height - textPoint.y - kheight ;
            
            if (rect.size.height - textPoint.y < kheight){
                rect.origin.y = rect.size.height - textPoint.y - kheight + viewFrameY;
            }
            else rect.origin.y = viewFrameY;
        }
        else {
            UITextView *newView = ((UITextView *)textField);
            textPoint = [newView convertPoint:CGPointMake(0, newView.frame.size.height + spacerY) toView:objectView];
            //            CGPoint textPoint = [newView convertPoint:CGPointMake(0, newView.frame.size.height + spacerY) toView:VCView];
            
            objViewSpaceY = rect.size.height - textPoint.y - kheight ;
            
            if (rect.size.height - textPoint.y < kheight){
                rect.origin.y = rect.size.height - textPoint.y - kheight + viewFrameY;
                
            }
            else rect.origin.y = viewFrameY;
            
        }
    }
    else rect.origin.y = viewFrameY;
    rect.size.height -= _tabbarHeight ;
    objectView.frame = rect;
    //    VCView.frame = rect;
    [UIView commitAnimations];
    //    }
    
    if ([self.boardDelegate respondsToSelector:@selector(keyboarDidshow:keyboardHeight:spaceY:) ]) {
        
        [self.boardDelegate keyboarDidshow:isShow  keyboardHeight:keyboardBounds.size.height spaceY:objViewSpaceY  ] ;
    }
}

//输入框获得焦点
- (id)firstResponder:(UIView *)navView {
    for (id aview in [self allSubviews:navView]) {
        if ([aview isKindOfClass:[UITextField class]] && [(UITextField *)aview isFirstResponder]) {
            return (UITextField *)aview;
        }
        else if ([aview isKindOfClass:[UITextView class]] && [(UITextView *)aview isFirstResponder]) {
            return (UITextView *)aview;
        }
    }
    return nil;
}

//找出所有的subview
- (NSArray *)allSubviews:(UIView *)theView {
    NSArray *results = [theView subviews];
    for (UIView *eachView in [theView subviews]) {
        NSArray *riz = [self allSubviews:eachView];
        if (riz) {
            results = [results arrayByAddingObjectsFromArray:riz];
        }
    }
    return results;
}

//输入框失去焦点，隐藏键盘
- (void)resignKeyboard:(UIView *)resignView {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

//设置previousBarItem或nextBarItem是否允许点击
- (void)checkBarButton:(id)textField {
    int i = 0,j = 0;
    UIButton *previousBarItem = [keyboardToolbar itemForIndex:0];
    UIButton *nextBarItem = [keyboardToolbar itemForIndex:1];
    for (id aview in [self allSubviews:objectView]) {
        if ([aview isKindOfClass:[UITextField class]] && ((UITextField*)aview).userInteractionEnabled && ((UITextField*)aview).enabled) {
            i++;
            if ([(UITextField *)aview isEqual:textField]) {
                j = i;
            }
        }
        else if ([aview isKindOfClass:[UITextView class]] && ((UITextView*)aview).userInteractionEnabled && ((UITextView*)aview).editable) {
            i++;
            if ([(UITextView *)aview isEqual:textField]) {
                j = i;
            }
        }
    }
    [previousBarItem setEnabled:j > 1 ? YES : NO];
    [nextBarItem setEnabled:j < i ? YES : NO];
}

//toolbar button点击事件
#pragma mark - UIKeyboardView delegate methods
-(void)toolbarButtonTap:(UIButton *)button {
    NSInteger buttonTag = button.tag;
    NSMutableArray *textFieldArray=[NSMutableArray arrayWithCapacity:10];
    for (id aview in [self allSubviews:objectView]) {
        if ([aview isKindOfClass:[UITextField class]] && ((UITextField*)aview).userInteractionEnabled && ((UITextField*)aview).enabled) {
            [textFieldArray addObject:(UITextField *)aview];
        }
        else if ([aview isKindOfClass:[UITextView class]] && ((UITextView*)aview).userInteractionEnabled && ((UITextView*)aview).editable) {
            [textFieldArray addObject:(UITextView *)aview];
        }
    }
    for (int i = 0; i < [textFieldArray count]; i++) {
        id textField = [textFieldArray objectAtIndex:i];
        if ([textField isKindOfClass:[UITextField class]]) {
            textField = ((UITextField *)textField);
        }
        else {
            textField = ((UITextView *)textField);
        }
        if ([textField isFirstResponder]) {
            if (buttonTag == 1) {
                if (i > 0) {
                    [[textFieldArray objectAtIndex:--i] becomeFirstResponder];
                    [self animateView:YES textField:[textFieldArray objectAtIndex:i] heightforkeyboard:kboardHeight];
                }
            }
            else if (buttonTag == 2) {
                if (i < [textFieldArray count] - 1) {
                    [[textFieldArray objectAtIndex:++i] becomeFirstResponder];
                    [self animateView:YES textField:[textFieldArray objectAtIndex:i] heightforkeyboard:kboardHeight];
                }
            }
        }
    }
    if (buttonTag == 3)
        [self resignKeyboard:objectView];
}


#pragma mark - TextField delegate methods
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self checkBarButton:textField];
    if ([self.boardDelegate respondsToSelector:@selector(alttextFieldDidBeginEditing:)]) {
        [self.boardDelegate alttextFieldDidBeginEditing:textField];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self.boardDelegate respondsToSelector:@selector(alttextFieldDidEndEditing:)]) {
        [self.boardDelegate alttextFieldDidEndEditing:textField];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{   // return NO to not change text
    
    if ( self.boardDelegate != nil && [ self.boardDelegate respondsToSelector:@selector(textField:shouldChangeTextInRange:replacementText:)] ) {
        return [self.boardDelegate textField:textField shouldChangeTextInRange:range replacementText:string ] ;
    }
    return YES;
}

#pragma mark - UITextView delegate methods
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if ( self.boardDelegate != nil && [ self.boardDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)] ) {
        return [self.boardDelegate textView:textView shouldChangeTextInRange:range replacementText:text ] ;
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if ([self.boardDelegate respondsToSelector:@selector(alttextViewDidEndEditing:)]) {
        [self.boardDelegate alttextViewDidEndEditing:textView];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([self.boardDelegate respondsToSelector:@selector(alttextViewWillBeginEditing:)]) {
        [self.boardDelegate alttextViewWillBeginEditing:textView];
    }
    return YES ;
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([self.boardDelegate respondsToSelector:@selector(alttextViewDidBeginEditing:)]) {
        [self.boardDelegate alttextViewDidBeginEditing:textView];
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    if ([self.boardDelegate respondsToSelector:@selector(textViewDidChange:)]) {
        [self.boardDelegate textViewDidChange:textView];
    }
}

@end

@implementation UIKeyboardViewController (UIKeyboardViewControllerCreation)

- (id)initWithControllerDelegate:(id <UIKeyboardViewControllerDelegate>)delegateObject {
    if (self = [super init]) {
        self.boardDelegate = delegateObject;
        if ([self.boardDelegate isKindOfClass:[UIViewController class]]) {
            //夏冰修改再次预约视图上升bug。
            objectView = [(UIViewController *)[self boardDelegate] view];
            
            //            UIViewController * VC = (UIViewController *)self.boardDelegate;
            //           JZLog(@"presentVC.Class = %@",VC.parentViewController.class);
            //            if (![VC.parentViewController isKindOfClass:[UINavigationController class]]) {
            //                VCView = VC.parentViewController.view;
            //            }else {
            //                VCView = [(UIViewController *)[self boardDelegate] view];
            //            }
        }
        else if ([self.boardDelegate isKindOfClass:[UIView class]]) {
            objectView = (UIView *)[self boardDelegate];
            //            VCView = (UIView *)[self boardDelegate];
        }
        //不适合每次显示栏再记录，所以初始化的时候记录
        viewFrameY = objectView.frame.origin.y;
        //        viewFrameY = VCView.frame.origin.y;
        [self addKeyBoardNotification];
    }
    return self;
}

-(void) updateViewFrameY:(CGFloat) y{
    viewFrameY = y ;
}

@end

@implementation UIKeyboardViewController (UIKeyboardViewControllerAction)

//给键盘加上toolbar
- (void)addToolbarToKeyboard {
    keyboardToolbar = [[UIKeyboardView alloc] initWithFrame:CGRectMake(0, 0, objectView.frame.size.width, keyBoardToolbarHeight)];
    keyboardToolbar.delegate = self;
    NSArray *allSubview = [self allSubviews:objectView] ;
    for (id aview in allSubview) {
        if ([aview isKindOfClass:[UITextField class]]) {
            ((UITextField *)aview).inputAccessoryView = keyboardToolbar;
            ((UITextField *)aview).delegate = self;
        }
        else if ([aview isKindOfClass:[UITextView class]]) {
            ((UITextView *)aview).inputAccessoryView = keyboardToolbar;
            ((UITextView *)aview).delegate = self;
        }
    }
}
-(void)addTextFildOrTextViewDelegate
{
    NSArray *allSubview = [self allSubviews:objectView] ;
    for (id aview in allSubview) {
        if ([aview isKindOfClass:[UITextField class]]) {
            ((UITextField *)aview).inputAccessoryView = keyboardToolbar;
            ((UITextField *)aview).delegate = self;
        }
        else if ([aview isKindOfClass:[UITextView class]]) {
            ((UITextView *)aview).inputAccessoryView = keyboardToolbar;
            ((UITextView *)aview).delegate = self;
        }
    }
}

@end
